﻿using System.Windows;
using System.Windows.Controls;
using TfsComLib.TfsBranchLib2;

namespace TfsBranchLib2.View
{
    /// <summary>
    ///     Interaction logic for BranchSetPrecedentControl.xaml
    /// </summary>
    public partial class BranchSetPrecedentControl : UserControl
    {
        // Using a DependencyProperty as the backing store for PrecedentAggregator.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BranchSetItemValueProperty =
            DependencyProperty.Register("BranchSetItemValue", typeof (BranchSetItem), typeof (BranchSetPrecedentControl),
                new UIPropertyMetadata(null, OnChangeBranchSet));

        private BranchSetItemType itemType;

        public BranchSetPrecedentControl()
        {
            InitializeComponent();
        }

        public BranchSetItem BranchSetItemValue
        {
            get { return (BranchSetItem) GetValue(BranchSetItemValueProperty); }
            set { SetValue(BranchSetItemValueProperty, value); }
        }

        public BranchSetItemType ItemType
        {
            set
            {
                itemType = value;
                if (itemType == BranchSetItemType.AllBranches)
                {
                    plainText.Visibility = Visibility.Visible;
                    branchSetControl.Visibility = Visibility.Collapsed;
                    precedentViewGrid.Visibility = Visibility.Collapsed;
                }
                else
                {
                    plainText.Visibility = Visibility.Collapsed;
                    branchSetControl.Visibility = Visibility.Visible;
                    precedentViewGrid.Visibility = Visibility.Visible;
                }
            }
            get { return itemType; }
        }

        private static void OnChangeBranchSet(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var branchSetPrecedentControl = d as BranchSetPrecedentControl;
            if (branchSetPrecedentControl == null) return;
            var branchSetItem = e.NewValue as BranchSetItem;

            if (branchSetItem != null)
            {
                branchSetPrecedentControl.ItemType = branchSetItem.ItemType;
                if (branchSetItem.BranchSetValue != null)
                {
                    branchSetPrecedentControl.times.Text = branchSetItem.BranchSetValue.StringTimes;
                    branchSetPrecedentControl.ago.Text = branchSetItem.BranchSetValue.StringAgo;
                    branchSetPrecedentControl.branchSetControl.BranchSetValue =
                        branchSetItem.BranchSetValue.PrecedentValue;
                }
            }
            else
            {
                branchSetPrecedentControl.times.Text = string.Empty;
                branchSetPrecedentControl.ago.Text = string.Empty;
                branchSetPrecedentControl.branchSetControl.BranchSetValue = null;
                branchSetPrecedentControl.plainText.Visibility = Visibility.Collapsed;
                branchSetPrecedentControl.branchSetControl.Visibility = Visibility.Collapsed;
                branchSetPrecedentControl.precedentViewGrid.Visibility = Visibility.Collapsed;
            }
        }
    }
}